#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
template <class T>
struct ListNode
{
	ListNode* prev;
	ListNode* next;
	T data;

	ListNode(const T& val)
		: prev(nullptr)
		, next(nullptr)
		, data(val)
	{}
};

template <class T>
struct ListIterator
{
	typedef ListNode<T> Node;
	Node* pnode;

	ListIterator(Node* p)
		:pnode(p)
	{}

	T& operator*()
	{
		return pnode->data;
	}
	ListIterator<T>& operator++()
	{
		pnode = pnode->next;
		return *this;
	}

	bool operator!=(const ListIterator<T>& it)
	{
		return pnode != it.pnode;
	}
};

template <class T>
class List
{
	typedef ListNode<T> Node;
public:
	typedef ListIterator<T> iterator;

	iterator begin()
	{
		return iterator(head->next);
	}
	iterator end()
	{
		return iterator(head);
	}
	List()
	{
		head = new Node(T());
		head->prev = head;
		head->next = head;
	}
	void push_back(const T& val)
	{
		Node* newnode = new Node(val);
		Node* tail = head->prev;
		head->prev = newnode;
		tail->next = newnode;
		newnode->next = head;
		newnode->prev = tail;
	}
private:
	Node* head;
};
void test_list()
{
	List<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	List<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
int main()
{
	test_list();
	return 0;
}